<template>
  <RouterView :key="$route.fullPath"/>
</template>

<script setup>
import store from './store/index.js';
import API from './api/index.js';
import {watch, computed} from 'vue'

const currentUserId = computed(() => store.getters.currentUser);

watch(currentUserId, async () => {

  if (currentUserId.value == null) return ;

  const source = new EventSource(API.notifyUrl(currentUserId.value.id));
  source.onmessage = evt => {
    let official = 0, letter = 0, notify = 0;
    // 获取传回的信息
    let message = JSON.parse(evt.data);
    if (message.typeName === "官方") {
      official = 1;
    } else if (message.typeName === "私信") {
      letter = 1;
    } else {
      notify = 1;
    }
    store.commit("storeMessage", {official, letter, notify});
  }

  let ret = await API.queryUnReadMessages() ;
  if (ret.status) {
    store.commit("storeMessage", ret.data);
  }
}, {immediate: true })


</script>